package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.model.Coupon;
import com.cskaoyan.model.Topic;
import com.cskaoyan.model.TopicExample;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.WxTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WxTopicServiceImpl
 * @Description: 小程序专题模块实现类
 * @Author:YPS
 * @Date: 2022/5/9 17:23
 * @Version: 1.0
 **/
@Service
@Transactional
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    TopicMapper topicMapper;
    @Override
    public DataVO<WxTopicListVO> getTopicList(Integer page, Integer limit) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<Topic> topicList = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> pageInfo = new PageInfo<>(topicList);
        long total = pageInfo.getTotal();
        List<WxTopicListVO> wxTopicListVOS = new ArrayList<>();
        for (int i = 0; i < topicList.size(); i++) {
            WxTopicListVO wxTopicListVO = new WxTopicListVO();
            wxTopicListVO.setId(topicList.get(i).getId());
            wxTopicListVO.setPicUrl(topicList.get(i).getPicUrl());
            wxTopicListVO.setPrice(topicList.get(i).getPrice());
            wxTopicListVO.setReadCount(topicList.get(i).getReadCount());
            wxTopicListVO.setSubtitle(topicList.get(i).getSubtitle());
            wxTopicListVO.setTitle(topicList.get(i).getTitle());
            wxTopicListVOS.add(wxTopicListVO);
        }
        DataVO<WxTopicListVO> dataVO = new DataVO<>();
        dataVO.setLimit(limit);
        dataVO.setPage(page);
        dataVO.setPages(pageInfo.getPages());
        dataVO.setTotal((int) total);
        dataVO.setList(wxTopicListVOS);
        return dataVO;
    }

    @Override
    public DataVO<TopicTopicVO> getTopicRelated(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdNotEqualTo(id);
        PageHelper.startPage(1,4);
        List<Topic> topicList = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> pageInfo = new PageInfo<>(topicList);
        Long total = pageInfo.getTotal();
        List<TopicTopicVO> topicReadVOList = new ArrayList<>();
        for (Topic topic : topicList) {
            TopicTopicVO topicReadVO = new TopicTopicVO();
            topicReadVO.setAddTime(topic.getAddTime());
            topicReadVO.setContent(topic.getContent());
            topicReadVO.setDeleted(topic.getDeleted());
            topicReadVO.setGoods(topic.getGoods());
            topicReadVO.setId(topic.getId());
            topicReadVO.setPicUrl(topic.getPicUrl());
            topicReadVO.setPrice(topic.getPrice());
            topicReadVO.setReadCount(topic.getReadCount());
            topicReadVO.setSortOrder(topic.getSortOrder());
            topicReadVO.setSubtitle(topic.getSubtitle());
            topicReadVO.setTitle(topic.getTitle());
            topicReadVO.setUpdateTime(new Date());
            topicReadVOList.add(topicReadVO);
        }
        DataVO<TopicTopicVO> topicVODataVO = new DataVO<>();
        topicVODataVO.setList(topicReadVOList);
        topicVODataVO.setTotal(Math.toIntExact(total));
        topicVODataVO.setPage(1);
        topicVODataVO.setPages(pageInfo.getPages());
        topicVODataVO.setLimit(4);
        return topicVODataVO;
    }

}
